package com.idearworld.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.idearworld.beans.ErrorBean;
import com.idearworld.beans.GlobalConstants;
import com.idearworld.model.UserInfo;

public class ControllerUtil extends PropertyPlaceholderConfigurer{

	private static Map<String, Object> ctxPropertiesMap;
	
	public static UserInfo getLoginUser(HttpSession session){
		Object obj = session.getAttribute(GlobalConstants.LOGIN_USER);
		if (obj == null) {
			return null;
		}
		return (UserInfo)obj;
	}
	
	public static Integer getLoginUserId(HttpSession session){
		Object obj = session.getAttribute(GlobalConstants.LOGIN_USER);
		if (obj == null) {
			return null;
		}
		return ((UserInfo)obj).getUserId();
	}
	
	
   public static  Map<String,Object> successResult(Object value){
		
		Map<String,Object> result=new HashMap<String,Object>();
		result.put(GlobalConstants.REQUEST_SUCCESS, value);
		return result;
	}
	
   public static  Map<String,Object> failedResult(Integer errorType,String value){
		
		Map<String,Object> result=new HashMap<String,Object>();
		ErrorBean errorBean=new ErrorBean();
		errorBean.setErrorType(errorType);
		errorBean.setErrorMesg(value);
		result.put(GlobalConstants.REQUEST_FAILED,errorBean );
		return result;
	}
   
   @Override
 	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
 	        throws BeansException{
 		super.processProperties(beanFactoryToProcess, props);
 		ctxPropertiesMap = new HashMap<String, Object>();  
        for (Object key : props.keySet()) {  
            String keyStr = key.toString();  
            String value = props.getProperty(keyStr);  
            ctxPropertiesMap.put(keyStr, value);  
        }  
 	}
 	
 	public static Object getContextProperty(String name) {  
        return ctxPropertiesMap.get(name);  
    }
 	
 	public static String getSystemProperties(String name) { 
 		Object obj=ctxPropertiesMap.get(name);
        return obj==null ? "":String.valueOf(obj);  
    }
	
}
